package com.api.nosession;

import com.alibaba.fastjson.JSONObject;
import com.system.common.Result;
import com.system.security.IsSecurity;
import com.system.util.FfmpegUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.Arrays;

/**
 * @Classname VideoApi
 * @Description TODO
 * @Date 2023/11/3 10:36
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@IsSecurity(isSecurity = false)
@RequestMapping("video")
@RestController
public class VideoApi {


    @CrossOrigin(origins = "http://localhost:8848")
    @GetMapping("play")
    public void play(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.reset();

        // 在处理请求的方法中设置响应头部
        response.setHeader("Access-Control-Allow-Origin", "http://localhost:8848");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type");
        response.setHeader("Access-Control-Max-Age", "3600");

        File file = new File("D:\\我的文件\\mv\\Mono Inc.、Martin Engler - Children of the Dark.mp4");
        long fileLength = file.length();
        // 随机读文件
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");

        //获取从那个字节开始读取文件
        String rangeString = request.getHeader("Range");
        long range=0;
        if (StringUtils.isNotBlank(rangeString)) {
            range = Long.valueOf(rangeString.substring(rangeString.indexOf("=") + 1, rangeString.indexOf("-")));
        }
        //获取响应的输出流
        OutputStream outputStream = response.getOutputStream();
        //设置内容类型
      //  response.setHeader("Content-Type", "video/mp4");
        //返回码需要为206，代表只处理了部分请求，响应了部分数据
        response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);

        // 移动访问指针到指定位置
        randomAccessFile.seek(range);
        // 每次请求只返回1MB的视频流
        byte[] bytes = new byte[1024 * 1024];
        int len = randomAccessFile.read(bytes);

//        byte[] encryptedData = Base64.encodeBase64(Arrays.copyOfRange(bytes, 0, len)); // Base64加密


        //设置此次相应返回的数据长度
        response.setContentLength(len);
        //设置此次相应返回的数据范围
        response.setHeader("Content-Range", "bytes "+range+"-"+(fileLength-1)+"/"+fileLength);
        // 将这1MB的视频流响应给客户端
        outputStream.write(bytes, 0, len);
        outputStream.close();
        randomAccessFile.close();

        System.out.println("返回数据区间:【"+range+"-"+(range+len)+"】");
    }

    @GetMapping("m3u8")
    public JSONObject testPath(@RequestParam("path")String path) throws Exception {
        File file=new File(path);
        String parentPath=file.getParent();
        FfmpegUtil.mp4ToM3u8(path,parentPath+"/out");
        return Result.successInfo("成功");
    }

}
